
/**
 * Created by admin on 2017/10/26.
 */
$(document).ready(function() {
    var VAL = {
        NORMAL : "0000000",
        EVT_USER     : $("#userId").text(),
        T_MWPWHSMTRL  : "MWPWHSMTRL",
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }
    };
    var domObj = {
        $uploadForm : $("#uploadForm"),
        $uploadModel  :$("uploadModel"),
        buttons : {
            $import_btn 	: $("#import_btn"),
            $save_btn     	: $("#save_btn"),
            $select_btn    	: $("#select_btn"),
        },
        grids : {
            $prdListDiv 	: $("#prdListDiv"),
            $prdListGrd 	: $("#prdListGrd"),
            $prdListPg    	: $("#prdListPg"),
        }
    };
    var iniGridInfo = function(){
        var prdListCM = [
            {name: 'vrd_prd_seq_id',   index: 'vrd_prd_seq_id',    label:SHEET_ID_TAG,    width: 230},
            {name: 'mtrl_box_id',      index: 'mtrl_box_id',       label:PPBOX_ID_TAG,  width: 180},
            {name: 'box_cate',         index: 'box_cate',          label: BOX_CATE_TAG, width: 180},
            {name: 'cus_id_fk',        index: 'cus_id_fk',         label: CUS_ID_TAG,   width: 180},
            {name: 'mtrl_prod_id',     index: 'mtrl_prod_id',      label: MTRL_ID_TAG,  width: 180},
            {name: 'mtrl_grade',       index: 'mtrl_grade',         label: MTRL_GRADE, width: 180},
            {name: 'mtrl_def_code',    index: 'mtrl_def_code',     label: "来料不良代码", width: 180},
            {name: 'mtrl_pallet_id',   index: 'mtrl_pallet_id',    label: PALLET_ID_TAG, width: 180},
            {name: 'mtrl_lot_id',      index: 'mtrl_lot_id',        label: MTRL_LOT_ID_TAG, width: 180},
            {name: 'stb_shop',         index: 'stb_shop',           label: "来料仓别", width: 180},
            {name: 'storage_loc',      index: 'storage_loc',        label: "储位", width: 180},
            {name: 'crt_usr',          index: 'crt_usr',             label: CRT_USR_TAG, width: 180},
            {name: 'crt_timestamp',    index: 'crt_timestamp',    label: CRT_TIMESTAMP, width: 180}
        ];
        domObj.grids.$prdListGrd.jqGrid({
            url: "",
            datatype: "local",
            mtype: "POST",
            height: 400,
            autowidth: true,
            shrinkToFit:false,
            autoScroll: false,
            resizable: true,
            loadonce: true,
            viewrecords: true,
            rownumbers: true,
            rowNum: 20,
            multiselect : false,
            emptyrecords: true,
            colModel: prdListCM,
            pager: "#prdListPg"
        });

    };
    var btnFunc = {
        selectFileFunc  :function(){
            $("#uploadModel").modal("show");
        },
        saveFun: function () {
            var iary =[];
            var ids = domObj.grids.$prdListGrd.jqGrid('getDataIDs');
            for(var i=0;i<ids.length;i++) {
                var rowData = domObj.grids.$prdListGrd.jqGrid("getRowData", ids[i]);
                var iaryA={
                    vrd_prd_seq_id      : $.trim(rowData.vrd_prd_seq_id),
                    mtrl_box_id         : $.trim(rowData.mtrl_box_id),
                    box_cate            : $.trim(rowData.box_cate),
                    cus_id_fk           : $.trim(rowData.cus_id_fk),
                    mtrl_prod_id        : $.trim(rowData.mtrl_prod_id),
                    mtrl_grade          : $.trim(rowData.mtrl_grade),
                    mtrl_def_code       : $.trim(rowData.mtrl_def_code),
                    mtrl_pallet_id      : $.trim(rowData.mtrl_pallet_id),
                    mtrl_lot_id         : $.trim(rowData.mtrl_lot_id),
                    stb_shop            : $.trim(rowData.stb_shop),
                    storage_loc         : $.trim(rowData.storage_loc),
                    crt_usr             : $.trim(rowData.crt_usr),
                    crt_timestamp       : $.trim(rowData.crt_timestamp),
                }
                iary.push(iaryA);
            }
            var inObj = {
                trx_id : VAL.T_MWPWHSMTRL,
                action_flg : "A",
                evt_usr :VAL.EVT_USER,
                iary : iary
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("数据导入成功");
            }
        },
    };
    /**
     * Ini contorl's data
     */
var iniContorlData = function(){

    $('form').submit(function (event) {
        event.preventDefault();
        var form = $(this);
        var formData = new FormData(this);
        $.ajax({
            type: form.attr('method'),
            url: form.attr('action'),
            data: formData,
            mimeType: "multipart/form-data",
            contentType: false,
            cache: false,
            processData: false
        }).success(function (data) {
            var oary =  JSON.parse(JSON.parse(data));;
            setGridInfo(oary, domObj.grids.$prdListGrd);
            $("#uploadModel").modal("hide");
        }).fail(function (jqXHR, textStatus, errorThrown) {
            //错误信息
        });
    });
};

    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
        domObj.buttons.$import_btn.click(function(){
            btnFunc.selectFileFunc();
        });
        domObj.buttons.$save_btn.click(function(){
            btnFunc.saveFun();
        });
        $("#uploadModel").on("click","#uploadBtn",function() {
            domObj.$uploadForm.submit();
        });
    };

    /**
     * Other action bind
     */
    var otherActionBind = function(){

    };
    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
        iniGridInfo();
        iniContorlData();
        iniButtonAction();
        otherActionBind();
    };

    initializationFunc();
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth,offsetBottom1, divWidth1;
        divWidth = $("#prdListDiv").width();
        offsetBottom =  $(window).height() - $("#prdListDiv").offset().top;
        $("#prdListDiv").height(offsetBottom * 0.95);
        $("#prdListGrd").setGridWidth(divWidth * 0.98);
        $("#prdListGrd").setGridHeight(offsetBottom * 0.99 - 51);

    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});
